package com.msb.crm.service;

import com.msb.crm.base.BaseService;
import com.msb.crm.dao.ModuleMapper;
import com.msb.crm.dao.PermissionMapper;
import com.msb.crm.dao.RoleMapper;
import com.msb.crm.utils.AssertUtil;
import com.msb.crm.vo.Permission;
import com.msb.crm.vo.Role;
import com.msb.crm.vo.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class RoleService extends BaseService<Role,Integer> {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private ModuleMapper moduleMapper;


    /**
     * 查询角色列表
     * @return
     */
    public List<Map<String,Object>> queryAllRoles(Integer userId){
        return roleMapper.queryAllRoles(userId);
    }


    /**
     * 添加角色
     * @param role
     */
    @Transactional
    public void saveRole(Role role){
        //参数校验
        AssertUtil.isTrue(StringUtils.isBlank(role.getRoleName()),"角色名不能为空!");
        Role temp = roleMapper.queryRoleByRoleName(role.getRoleName());
        AssertUtil.isTrue(null!=temp,"该角色已存在!");
        //设置其他默认参数
        role.setIsValid(1);
        role.setCreateDate(new Date());
        role.setUpdateDate(new Date());
        //添加角色
        AssertUtil.isTrue(insertSelective(role)<1,"角色记录添加失败!");
    }

    /**
     * 更新角色
     * @param role
     */
    @Transactional
    public void updateRole(Role role){
        //判断id
        AssertUtil.isTrue(null == role.getId() || selectByPrimaryKey(role.getId()) == null,"待更新的记录不存在!");
        //判断参数的非空
        AssertUtil.isTrue(StringUtils.isBlank(role.getRoleName()),"角色名不能为空!");
        Role temp = roleMapper.queryRoleByRoleName(role.getRoleName());

        AssertUtil.isTrue(null != temp && !(temp.getId().equals(role.getId())),"该角色已存在!");
        role.setUpdateDate(new Date());
        AssertUtil.isTrue(updateByPrimaryKeySelective(role)<1,"角色记录更新失败!");
    }


    /**
     * 角色删除
     * @param roleId
     */
    @Transactional
    public void deleteRole(Integer roleId){
        //先根据传来的roleId找出角色对象
        AssertUtil.isTrue(null == roleId,"待删除记录不存在!");
        Role role = selectByPrimaryKey(roleId);
        AssertUtil.isTrue(null == role,"待删除记录不存在！");
        role.setIsValid(0);
        AssertUtil.isTrue(updateByPrimaryKeySelective(role)<1,"角色记录删除失败!");
    }


    /**
     * 添加权限记录
     * @param mids
     * @param roleId
     */
    public void addGrant(Integer[] mids,Integer roleId){
        /*判断角色是否存在权限表，有就全部删除，再根据所选全部添加*/
        Role role = selectByPrimaryKey(roleId);
        AssertUtil.isTrue(null == roleId || null == role,"待授权的角色不存在!");
        int count = permissionMapper.countPermissionByRoleId(roleId);
        if (count > 0){
            AssertUtil.isTrue(permissionMapper.deletePermissionsByRoleId(roleId)<count,"权限分配失败!");
        }
        if (null != mids && mids.length > 0){
            List<Permission> permissions = new ArrayList<>();
            for (Integer mid : mids){
                Permission permission = new Permission();
                permission.setCreateDate(new Date());
                permission.setUpdateDate(new Date());
                permission.setModuleId(mid);
                permission.setRoleId(roleId);
                permission.setAclValue(moduleMapper.selectByPrimaryKey(mid).getOptValue());
                permissions.add(permission);
            }
           AssertUtil.isTrue( permissionMapper.insertBatch(permissions) != permissions.size(),"角色授权失败!");
        }
    }

}
